#!/bin/bash
changeipaddress(){
    changeip=$(whiptail --title "更改IP" --inputbox "请输入新的IP地址" 10 60 `hostname -I` 3>&1 1>&2 2>&3)
    exitstatus=$?
    if [ $exitstatus = 0 ]; then
        whiptail --title "Message" --msgbox "IP地址将由\n$(hostname -I)\n改为:\n$changeip\n" 10 60
        #判断IP是否
        if echo $changeip | grep "^\([0-9]\{1,3\}\.\)\{3\}[0-9]\{1,3\}$";then
            #判断文件是否规范
            [ ! -e /etc/sysconfig/network-scripts/ifcfg-eth0 ]   && echo -e "\n网卡配置文件不规范，请检查 ：\n  /etc/sysconfig/network-scripts/ifcfg-eth0\n" &&  exit 1
            #判断IP是否可用
            ping -c 2 $changeip  > /dev/null && echo -e "\n[$changeip]\n 该IP已在使用中，请检查\n" && exit 1 || echo "该IP可用"
            #更新IP地址
            #       echo "$(hostname -I) >>> $changeip"
            sed -i 's/dhcp/static/i'  /etc/sysconfig/network-scripts/ifcfg-eth0
            grep ^IPADDR  /etc/sysconfig/network-scripts/ifcfg-eth0 &&    sed -i "s/^IPADDR=.*/IPADDR=$changeip/g"  /etc/sysconfig/network-scripts/ifcfg-eth0 || echo "IPADDR="$changeip >>/etc/sysconfig/network-scripts/ifcfg-eth0
            sed -i "s/^NETMASK=.*//g" /etc/sysconfig/network-scripts/ifcfg-eth0
            grep ^PREFIX  /etc/sysconfig/network-scripts/ifcfg-eth0 &&  sed -i "s/^PREFIX=.*/PREFIX=24/g" /etc/sysconfig/network-scripts/ifcfg-eth0 || echo "PREFIX=24" >>/etc/sysconfig/network-scripts/ifcfg-eth0
            grep ^GATEWAY /etc/sysconfig/network-scripts/ifcfg-eth0 &&  sed -i "s/^GATEWAY=.*/GATEWAY=$(echo $changeip|awk -F'.' '{print $1"."$2"."$3}').254/g" /etc/sysconfig/network-scripts/ifcfg-eth0||echo "GATEWAY="$(echo $changeip | awk -F'.' '{print $1"."$2"."$3}')".254" >>/etc/sysconfig/network-scripts/ifcfg-eth0
            grep ^DNS=  /etc/sysconfig/network-scripts/ifcfg-eth0 &&  sed -i "s/^DNS=.*//g" /etc/sysconfig/network-scripts/ifcfg-eth0
            grep ^DNS1=  /etc/sysconfig/network-scripts/ifcfg-eth0 &&  sed -i "s/^DNS1=.*/DNS1=100.100.2.136/g" /etc/sysconfig/network-scripts/ifcfg-eth0 || echo "DNS1=100.100.2.136" >>/etc/sysconfig/network-scripts/ifcfg-eth0
            grep ^DNS2=  /etc/sysconfig/network-scripts/ifcfg-eth0 &&  sed -i "s/^DNS2=.*/DNS2=100.100.2.138/g" /etc/sysconfig/network-scripts/ifcfg-eth0 || echo "DNS2=100.100.2.138" >>/etc/sysconfig/network-scripts/ifcfg-eth0
            grep ^DNS3=  /etc/sysconfig/network-scripts/ifcfg-eth0 &&  sed -i "s/^DNS3=.*/DNS3=114.114.114.114/g" /etc/sysconfig/network-scripts/ifcfg-eth0 || echo "DNS3=114.114.114.114" >>/etc/sysconfig/network-scripts/ifcfg-eth0

	    #修改过zabbix-agent的Hostname，是否修改看你Hostname是怎么规划的。如果不是写的本机IP地址，就可以不用更改。
            sed -i "s/^Hostname=.*/Hostname=$changeip/g"  /etc/zabbix/zabbix_agentd.conf
            systemctl restart zabbix-agent

            echo -e "\n修改完毕，请手动重启网卡:\n    systemctl restart network\n"
            #systemctl restart network
        else
            echo "输入的IP不合法"
        fi
    else
        Entry_Menu_Bar
    fi
}
